$(function() {
	  $('#summernote').summernote({
	    lang: 'zh-CN',
	   	 width:1200,
	     height:800,
	     disableDragAndDrop: true,

 		 callbacks : { 
 		 	onImageUpload : function(files) { 
 				 sendFile(files,this);
 		 	}
 		 }
 	});	 
 	
 	$('#summernote_food').summernote({
	    lang: 'zh-CN',
	   	 width:1200,
	     height:800,
	     disableDragAndDrop: true,

 		 callbacks : { 
 		 	onImageUpload : function(files) { 
 				 sendFile(files,this);
 		 	}
 		 }
 	});	 
 	
 	$('#summernote_route').summernote({
	    lang: 'zh-CN',
	   	 width:1200,
	     height:800,
	     disableDragAndDrop: true,

 		 callbacks : { 
 		 	onImageUpload : function(files) { 
 				 sendFile(files,this);
 		 	}
 		 }
 	});	 
	  
});

	
function sendFile(files,obj) {   
     var data = new FormData();
     data.append("file", files[0]);
     
 	$.ajax({
            data :data,  
            type : "post",  
            url : "/richtext/upload", 
            cache : false,  
            contentType : false,  
            processData : false,  
            dataType : 'text',
            beforeSend:function(){  },
            success: function(response) { 
            	$(obj).summernote('insertImage', response);  
                 if(response=='nologin'){
                    	location.href='/adminlogin'
                    }
            },  
            error:function(){  
                alert("上传失败");  
            }  
        });  
    }  

function deletepic(obj){
	if(window.confirm("确定删除该图片吗")){
			var file=$(obj).children().attr('src')
			$.ajax({
				 url: '/deletefile',  
		         type: "post",
		         data:{ file:file
		         },
		         dataType:"text",
		         beforeSend:function(){},
		         success: function(data) {
		         	$(obj).children().attr('src','');
		         	 if(data=='nologin'){
                    	location.href='/adminlogin'
                    }
		         },
		         error:function(){
		         	alert('删除失败')
		         }
			})
		}else{			
		}	
}


function upload(){           
        $("#uploadform").ajaxSubmit({

                url: '/upload',  
                type: "post",
                data:{},
                dataType:"text",
                beforeSend:function(){},
                success: function(data) {
                	var boo=false;
                	$(".pic").each(function(){
                		boo=$(this).attr("src")==""
                		if(boo){ return false }
                	})
                	if(boo){
                		$(".pic").each(function(){
                		while($(this).attr("src")==""){
                			$(this).attr("src",data)
                				return false;
                			}
                		})
                	}else{
                		$('.pro_pic').append('<a href="JavaScript:void()" onclick="deletepic(this)"><img src = '+data+'  class="img-thumbnail pic" ></a>')
                	}
                	
                	/*$(".pic").each(function(){
                		while($(this).attr("src")==""){
                			$(this).attr("src",data)
                			return false;
                		}
                	})*/
                    //遍历同一个class如果没有src那么赋值，return false                    
                    if(data=='nologin'){
                    	location.href='/adminlogin'
                    }
                },
                error:function(){
                	alert("上传出错了！")
                }
        });  
}
function save(){
	$('[name=detail]:input').val($('#summernote').summernote('code'))
	$('[name=food]:input').val($('#summernote_food').summernote('code'))
	$('[name=route]:input').val($('#summernote_route').summernote('code'))
	
	var imageArr='';
	$('.pic').each(function(){
		var src=$(this).attr('src')
		if(src!=''){
			imageArr +=$(this).attr('src')+',';
		}
	})
	var srcResult=imageArr.substring(0,imageArr.length-1)
	$('[name=subImages]:input').val(srcResult)
	
	$.ajax({
		url:'/update/product',
		type:'post',
		data:$('#uploadform').serialize(),
		success:function(response){
			if(response=='succ'){
				alert('保存成功！')
			}
			if(response=='erro'){ alert('保存失败！请稍后再试') }
			if(response=='nologin'){
				location.href='/adminlogin'
			}
		},
		error:function(){
			alert('请求出错！')
		}
	})
	

}
